package com.daoxiang.controller;

import com.daoxiang.pojo.Goods;
import com.daoxiang.service.GoodsService;
import com.daoxiang.utils.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @ResponseBody
    @GetMapping("/queryAllGoods")
    public List<Goods> queryAllGoods(Goods goods) {
        List<Goods> goodsList = this.goodsService.queryGoods(goods);
        return goodsList;
    }
    @ResponseBody
    @GetMapping("/queryGoodsByKey")
    public List<Goods> queryGoodsByKey(@RequestParam("gname")String gname){
//        System.out.println(gname);
        List<Goods> goodsList = this.goodsService.queryGoodsByKey(gname);
        return goodsList;
    }
    @ResponseBody
    @PutMapping("/updateGoodById")
    public String updateGoodById(Goods goods,@RequestParam("file_image")MultipartFile multipartFile){
//        System.out.println(goods.toString());
        // 调用utils层的方法进行文件上传处理进行查询操作
        MyUtils.doSingleFileUpload(multipartFile,"goodsUpdateFile","goodsInfo-",goods,"image");
        // 调用service层进行更新操作
        this.goodsService.updateGoodById(goods);
        return "ok";
    }
    @ResponseBody
    @DeleteMapping("/deleteGoodById")
    public String deleteGoodById(@RequestParam("gid")Integer gid){
        System.out.println(gid);
        this.goodsService.deleteGoodById(gid);
        return "ok";
    }
    @ResponseBody
    @PostMapping("/insertGood")
    public String insertGood(Goods goods,@RequestParam("file_image")MultipartFile multipartFile){
        // 调用utils层的方法进行文件上传处理进行查询操作
        MyUtils.doSingleFileUpload(multipartFile,"goodsInfoUpdateFile","goodsInfo-",goods,"image");
        this.goodsService.insertGood(goods);
        return "ok";
    }

    @GetMapping("/queryGoodsByName")
    public ResponseEntity<List<Goods>> queryGoodsByName(String searchValue) {
        List<Goods> goodsList = this.goodsService.queryGoodsByName(searchValue);
        return new ResponseEntity<List<Goods>>(goodsList, HttpStatus.OK);
    }
}
